<template>
	<div class="system-post-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="postDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="state.rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="岗位名称" prop="postName">
							<el-input v-model="state.ruleForm.postName" placeholder="请输入岗位名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="岗位编码" prop="postCode">
							<el-input v-model="state.ruleForm.postCode" placeholder="请输入岗位编码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="岗位排序" prop="postSort">
							<el-input-number v-model="state.ruleForm.postSort" :min="0" :max="999" controls-position="right"
								placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="岗位状态">
							<el-radio-group v-model="state.ruleForm.status">
								<el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{
									dict.label }}</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.ruleForm.remark" type="textarea" placeholder="请输入备注"
								clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default" v-auth="'system:post:edit'">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemPostDialog">
import { reactive, ref } from 'vue';
import { usePost } from '/@/api/system/post';
import { ElMessage } from 'element-plus';
import { useDict } from '/@/utils/dict';

const { sys_normal_disable } = useDict("sys_normal_disable");
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const postDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		createBy: "",
		createTime: "",
		updateBy: "",
		updateTime: '',
		postId: 0,
		postCode: "",
		postName: "",
		postSort: 0,
		status: "0",
		remark: "",
		flag: false
	},
	rules: {
		postName: [
			{ required: true, message: "请输入岗位名称", trigger: "blur" },
			{ min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur" },
		],
		postCode: [
			{ required: true, message: "请输入岗位编码", trigger: "blur" },
			{ min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur" },
		],
		postSort: [
			{ required: true, message: "请输入岗位排序", trigger: "blur" },
		],
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: RowPostType, total: number) => {
	state.dialog.type = type;
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改岗位';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增岗位';
		state.dialog.submitTxt = '新 增';
		getPostRuleForm();
		state.ruleForm.postSort = total + 1;

	}
	state.dialog.isShowDialog = true;
};

// 提交
const onSubmit = async () => {
	try {
		await postDialogFormRef.value.validate(async (valid: any, fields: any) => {
			if (valid) {
				if (state.dialog.type === 'add') {
					await addPost();
				} else {
					await updatePost();
				}
			} else {
				ElMessage.error('请检查输入项是否正确');
				return Promise.reject(fields);
			}
		});
	} catch (err) {
		Promise.reject(err);
	}
};
const addPost = async () => {
	try {
		const res = await usePost().addPost({
			postCode: state.ruleForm.postCode,
			postName: state.ruleForm.postName,
			postSort: state.ruleForm.postSort,
			status: state.ruleForm.status,
			remark: state.ruleForm.remark,
		});
		if (res.code === 200) {
			ElMessage.success(res.msg);
			closeDialog();
			emit('refresh');
		} else {
			ElMessage.error(res.msg);
		}
	} catch (err) {
		Promise.reject(err);
	}
};
const updatePost = async () => {
	try {
		const res = await usePost().updatePost(state.ruleForm);
		if (res.code === 200) {
			ElMessage.success(res.msg);
			closeDialog();
			emit('refresh');
		} else {
			ElMessage.error(res.msg);
		}
	} catch (err) {
		Promise.reject(err);
	}
};


// 初始化岗位数据
const getPostRuleForm = () => {
	state.ruleForm = {
		createBy: "",
		createTime: "",
		updateBy: "",
		updateTime: '',
		postId: 0,
		postCode: "",
		postName: "",
		postSort: 0,
		status: "0",
		remark: "",
		flag: false
	};
};
// 取消
const onCancel = () => {
	closeDialog();
	getPostRuleForm();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};// 暴露变量
defineExpose({
	openDialog,
});
</script>